<template>
    <div class="login">
        <div class="lTitle">
            <img src="../../static/images/logo3.png" alt="" />

            <h2>login</h2>
        </div>

        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="user_acc" name="用户名" label="用户名" placeholder="用户名" :rules="[{ required: true, message: '请填写用户名' }]" />
                <van-field v-model="user_pwd" type="password" name="密码" label="密码" placeholder="密码" :rules="[{ required: true, message: '请填写密码' }]" />
            </van-cell-group>
            <div style="margin: 16px">
                <van-button round block type="primary" native-type="submit"> 登录 </van-button>
            </div>
        </van-form>
        <router-link class="goRegister" replace to="/register">去注册</router-link>
    </div>
</template>
<script setup>
import { Form, Field, CellGroup } from "vant";
import { ref, onMounted } from "vue";
import { login, image_code, selectTokenUser } from "@/request/index";
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";
let router = useRouter();
let user_acc = ref("");
let user_pwd = ref("");
const store = useStore();
const onSubmit = () => {
    // console.log(user_acc.value, user_pwd.value);
    login({ user_acc: user_acc.value, user_pwd: user_pwd.value }).then((data) => {
        alert(data.msg);
        // console.log(data);
        // console.log(data.token);

        if (data.code == 200) {
            localStorage.setItem("autoToken", data.token);
            localStorage.setItem("token", data.res[0].user_id);
            selectTokenUser({ user_id: data.res[0].user_id })
                .then((data) => {
                    return data;
                })
                .then((content) => {
                    if (content.code == 200) {
                        // store.state.myRoute.loginObj = content.result[0];
                        store.commit("myRoute/setLoginObj", content.result[0]);
                        console.log(store.state.myRoute.loginObj, 5656);
                        router.go(-1);
                    }
                });
        }
    });
};
</script>
<style lang="scss" scope>
@import url(../../static/style/reset.css);
@import "../../static/style/variable.scss";
.login {
    overflow: hidden;
    .lTitle {
        font-size: 1.28rem;
        text-align: center;
        padding-bottom: 1.3333rem;
        margin-top: 3.2rem;
        font-weight: 800;
        color: $activeColor;
        display: flex;
        justify-content: center;
        img {
            width: 1.3333rem;
            margin-right: 8px;
        }
    }
    .goRegister {
        display: block;
        font-size: 0.32rem;
        text-align: center;
    }
    .van-button--primary {
        color: white;
        background-color: $bgColor;
        border: 1px solid $activeColor;
        width: 100%;
        margin: 0 auto;
    }
    .van-form {
        margin: 0 0.2667rem;
    }
}
</style>
